﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeathLock
{
    internal class Program
    {
        private static readonly object _locaA = new object();
        private static readonly object _locaB = new object();

        static void Main(string[] args)
        {
            new Task(() =>
            {
                lock (_locaA)
                {
                    Console.WriteLine($"线程{Thread.CurrentThread.ManagedThreadId}获得A锁");
                    Thread.Sleep(1000);
                    lock (_locaB)
                    {
                        Console.WriteLine($"线程{Thread.CurrentThread.ManagedThreadId}获得B锁");
                    }
                }
            }).Start();

            new Task(() =>
            {
                lock (_locaB)
                {
                    Console.WriteLine($"线程{Thread.CurrentThread.ManagedThreadId}获得B锁");
                    Thread.Sleep(1000);
                    lock (_locaA)
                    {
                        Console.WriteLine($"线程{Thread.CurrentThread.ManagedThreadId}获得A锁");
                    }
                }
            }).Start();

            Thread.Sleep(-1);
        }
    }
}
